using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Events;

namespace PKGames
{
	public class WindowTransition : ScriptableObject
	{
		protected AbstractWindow _lastWindow;

		protected AbstractWindow _nextWindow;

		public UnityEvent OnFinish;

		private Action<AbstractWindow, AbstractWindow> callback;

		public void InitializeTransition(AbstractWindow lastWindow, AbstractWindow nextWindow)
		{
			_lastWindow = lastWindow;
			_nextWindow = nextWindow;
		}

		public void InitOpenWindowTransition(Action<AbstractWindow, AbstractWindow> callback, RectTransform targetTransform)
		{
			if (!_nextWindow)
			{
				UnityEngine.Debug.LogError("Transition could not be completed.");
				return;
			}
			this.callback = callback;
			WindowTransitionController.Instance.StartCoroutine(BeginOpenWindowTransition(targetTransform));
		}

		public void InitClosePageTransition(Action<AbstractWindow, AbstractWindow> callback, RectTransform targetTransform)
		{
			if (_lastWindow == null)
			{
				UnityEngine.Debug.LogError("Transition could not be completed.");
				return;
			}
			this.callback = callback;
			WindowTransitionController.Instance.StartCoroutine(BeginCloseWindowTransition(targetTransform));
		}

		public virtual IEnumerator BeginOpenWindowTransition(RectTransform targetTransform)
		{
			yield return null;
			FinishTransition();
		}

		public virtual IEnumerator BeginCloseWindowTransition(RectTransform targetTransform)
		{
			yield return null;
			FinishTransition();
		}

		public void FinishTransition()
		{
			callback(_lastWindow, _nextWindow);
			OnFinish.Invoke();
		}
	}
}
